package net.starwifi.duobao.directive;


import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.starwifi.duobao.entity.Setting;
import net.starwifi.duobao.service.SettingService;
import net.starwifi.duobao.template.directive.BaseDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 模板指令 - config指令读取.
 *
 * @author <a href="http://ruoo.whosenet.com">ruoo.whosenet.com</>
 * @version 1.0
 */
@Component("settingDirective")
public class SettingDirective extends BaseDirective {

    /** 变量名称 */
    private static final String VARIABLE_NAME = "setting";

    @Autowired
    private SettingService settingService;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        String code=params.get("code").toString();
        Setting setting=settingService.findByCode(code);
        if(setting!=null){
            env.getOut().write(setting.getConfigValue());
        }
    }

}